package cq.zhcqk.activity;

import android.app.ActionBar;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.dou361.dialogui.DialogUIUtils;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;

import org.apache.http.Header;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import cq.zhcqk.R;
import cq.zhcqk.adapter.DyxxAdapter;
import cq.zhcqk.adapter.DyxxDetailAdapter;
import cq.zhcqk.adapter.DzbJiaGouAdapter;
import cq.zhcqk.base.BaseActivity;
import cq.zhcqk.util.Common;
import cq.zhcqk.util.GsonUtil;
import cq.zhcqk.util.StringUtil;

/**
 * Created by zhangle on 2017/12/9.
 */
public class DY_Dyxx_DetailActivity extends BaseActivity implements View.OnClickListener{
    private String titleTxt;
    private int cid;

    private List<Map<String,Object>> list;
    private List<Map<String,Object>> listDz;
    private DyxxDetailAdapter dyxxDetailAdapter;
    private ListView listView;
    private TextView dz_title;

    /** popup窗口里的ListView */
    private ListView mTypeLv;
    /** 数据适配器 */
    private ArrayAdapter<String> testDataAdapter;
    /** popup窗口 */
    private PopupWindow typeSelectPopup;
    /** 模拟的假数据 */
    private List<String> listPop;

    @Override
    protected void initView() {
        setContentView(R.layout.activity_ac_dyxx_detail);
        dz_title = (TextView) findViewById(R.id.dz_title);
        titleTxt =  getIntent().getStringExtra("title");
        cid = getIntent().getIntExtra("cid",-1);
        initBack();
        initListView();

        dz_title.setOnClickListener(this);
    }

    @Override
    protected void initData() {
        getZbDetail();
    }

    private void initBack(){
        ImageView back = (ImageView) findViewById(R.id.header_back);
        TextView title = (TextView) findViewById(R.id.header_text);
        title.setText(titleTxt+"");
        back.setOnClickListener(this);
    }

    private void initListView(){
        listView = (ListView) findViewById(R.id.dyxx_detail_list);
        list = new ArrayList<>();
        dyxxDetailAdapter = new DyxxDetailAdapter(context,list);
        listView.setAdapter(dyxxDetailAdapter);
    }

    private void getZbDetail(){
        buildBean.show();
        RequestParams params = new RequestParams();
        httpClient.post(Common.URL+Common.NEWS_DEPTTREE,params,new JsonHttpResponseHandler(){
            @Override
            public void onSuccess(int statusCode, Header[] headers,
                                  JSONObject response) {
                super.onSuccess(statusCode, headers, response);
                System.out.println("onSuccess："+response);
                Map<String, Object> map = GsonUtil.GsonToMaps(response+"");
                listDz = (List<Map<String, Object>>) map.get("data");
                if (listDz!=null&&listDz.size()>0){
                    listPop = new ArrayList<String>();
                    for (int i=0;i<listDz.size();i++){
                        listPop.add(listDz.get(i).get("name")+"");
                    }
                    initSelectPopup();
                    dz_title.setText(listDz.get(0).get("name")+"");
                    getDetail(StringUtil.stringToInt(listDz.get(0).get("id")+"")+"");
                }
            }

            @Override
            public void onFailure(int statusCode, Header[] headers,
                                  Throwable throwable, JSONObject errorResponse) {
                super.onFailure(statusCode, headers, throwable,
                        errorResponse);
                Toast.makeText(context,"获取失败，请稍后再试",Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                super.onFailure(statusCode, headers, responseString, throwable);
                Toast.makeText(context,"获取失败，请稍后再试",Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onFinish() {
                super.onFinish();
                DialogUIUtils.dismiss(buildBean);
            }
        });
    }

    private void getDetail(String dpid){
        buildBean.show();
        RequestParams params = new RequestParams();
        params.add("cid",cid+"");
        params.add("deptid",dpid);
        httpClient.post(Common.URL+Common.PARTYSTUDY,params,new JsonHttpResponseHandler(){
            @Override
            public void onSuccess(int statusCode, Header[] headers,
                                  JSONObject response) {
                super.onSuccess(statusCode, headers, response);
                System.out.println("onSuccess："+response);
                Map<String, Object> map = GsonUtil.GsonToMaps(response+"");
                List<Map<String,Object>> listNew = (List<Map<String, Object>>) map.get("data");
//                list.addAll(listNew);
                dyxxDetailAdapter = new DyxxDetailAdapter(context,listNew);
                listView.setAdapter(dyxxDetailAdapter);
            }

            @Override
            public void onFailure(int statusCode, Header[] headers,
                                  Throwable throwable, JSONObject errorResponse) {
                super.onFailure(statusCode, headers, throwable,
                        errorResponse);
                System.out.println("errorResponse："+errorResponse);
                Toast.makeText(context,"获取失败，请稍后再试",Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                super.onFailure(statusCode, headers, responseString, throwable);
                Toast.makeText(context,"获取失败，请稍后再试",Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onFinish() {
                super.onFinish();
                DialogUIUtils.dismiss(buildBean);
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.header_back:
                finish();
                break;
            case R.id.dz_title:
                // 使用isShowing()检查popup窗口是否在显示状态
                if (typeSelectPopup != null && !typeSelectPopup.isShowing()) {
                    typeSelectPopup.showAsDropDown(dz_title, 0, 10);
                }
                break;
        }
    }

    /**
     * 初始化popup窗口
     */
    private void initSelectPopup() {
        mTypeLv = new ListView(this);
        // 设置适配器
        testDataAdapter = new ArrayAdapter<String>(this, R.layout.popup_text_item, listPop);
        mTypeLv.setAdapter(testDataAdapter);

        // 设置ListView点击事件监听
        mTypeLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                // 在这里获取item数据
                String value = listPop.get(position);
                // 把选择的数据展示对应的TextView上
                dz_title.setText(value);
                getDetail(StringUtil.stringToInt(listDz.get(position).get("id")+"")+"");
                // 选择完后关闭popup窗口
                typeSelectPopup.dismiss();
            }
        });
        typeSelectPopup = new PopupWindow(mTypeLv, dz_title.getWidth(), ActionBar.LayoutParams.WRAP_CONTENT, true);
        // 取得popup窗口的背景图片
        Drawable drawable = ContextCompat.getDrawable(this, R.drawable.bg_corner);
        typeSelectPopup.setBackgroundDrawable(drawable);
        typeSelectPopup.setFocusable(true);
        typeSelectPopup.setOutsideTouchable(true);
        typeSelectPopup.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                // 关闭popup窗口
                typeSelectPopup.dismiss();
            }
        });
    }
}
